%pylab inline
from matplotlib import animation
from IPython.display import HTML
def wave(x,t,k0,c,sigmak,pkmax,nstep):
total=0
dk=2*pkmax*sigmak/nstep
for i in range(nstep):
k=k0 - pkmax *sigmak+i*dk
omega=c*k + 1 * (k-k0)*(k-k0)
total += exp(-(k-k0)**2/sigmak)*exp(1j*(k*x-omega*t))*dk
# print (i,'k=',k)
return (total.real)
# k0,c,sigmak,pkmax,nstep
params=[1, .2, 0.1, 5, 20]
wave(2.7, 10, *params)
k0=1
sigmak=0.02*k0
c=0.1
pkmax=5
kstep=10
xx=10
xmin=-xx*2*pi/k0
xmax=xx*2*pi/k0
xstep=300
nstep=20
params=[k0,c,sigmak,pkmax,nstep]
ymax=wave(0, 0, k0, c,sigmak, pkmax, kstep)
x = np.linspace(xmin, xmax, xstep)
y=wave(x, 0, k0, c,sigmak, pkmax, kstep)
print('ymax =',ymax)
plot(x,y);
%%capture
# create a simple animation
fig = plt.figure()
ax = plt.axes(xlim=(xmin,xmax), ylim=(-ymax, ymax))
line, = ax.plot([], [], lw=2)
def init():
line.set_data([], [])
return line,
def animate(i):
line.set_data(x, wave(x, i,*params))
return line,
HTML(animation.FuncAnimation(fig, animate, init_func=init,
frames=linspace(-300,700,60),interval=20, blit=True).to_jshtml())